package com.jic.order.impl;

import com.jic.common.base.vo.Page;
import com.jic.order.mapper.OrderRouteSplitWarehouseConditionMapper;
import com.jic.order.mapper.OrderRouteSplitWarehouseMapper;
import com.jic.order.request.OrderRouteSplitWarehouseAndConditionsRequest;
import com.jic.order.request.OrderRouteSplitWarehouseConditionRequest;
import com.jic.order.request.OrderRouteSplitWarehouseRequest;
import com.jic.order.response.OrderRouteSplitWarehouseResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderRouteSplitWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 路由分仓规则表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-03 10:55:11
 */
@Service
public class OrderRouteSplitWarehouseServiceImpl implements OrderRouteSplitWarehouseService {

    @Autowired
    private OrderRouteSplitWarehouseMapper orderRouteSplitWarehouseMapper;

    @Autowired
    private OrderRouteSplitWarehouseConditionMapper orderRouteSplitWarehouseConditionMapper;

    @Override
    @Transactional
    public long insert(OrderRouteSplitWarehouseAndConditionsRequest orderRouteSplitWarehouse) {
        //新增分仓规则
        orderRouteSplitWarehouse.setId(null);
        orderRouteSplitWarehouseMapper.insert(orderRouteSplitWarehouse);
        //新增分仓规则匹配条件
        List<OrderRouteSplitWarehouseConditionRequest> condition = orderRouteSplitWarehouse.getMatchCondition();
        for (OrderRouteSplitWarehouseConditionRequest orderRouteSplitWarehouseConditionRequest : condition) {
            if (orderRouteSplitWarehouseConditionRequest != null) {
                //todo
                orderRouteSplitWarehouseConditionRequest.setCreator(orderRouteSplitWarehouse.getCreator());
                orderRouteSplitWarehouseConditionRequest.setUpdater(orderRouteSplitWarehouse.getCreator());
                orderRouteSplitWarehouseConditionRequest.setRouteSplitWarehouseId(orderRouteSplitWarehouse.getId());
            }
        }
        int flag = orderRouteSplitWarehouseConditionMapper.insertForeach(condition);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return orderRouteSplitWarehouseMapper.delete(id);
    }

    @Override
    public Boolean editStatus(String ids) {
        String[] arrId=ids.split(",");
        for(int i=0;i<arrId.length;i++){
            orderRouteSplitWarehouseMapper.editStatus(Long.valueOf(arrId[i]));
        }
        return true;
    }

    @Override
    @Transactional
    public long update(OrderRouteSplitWarehouseAndConditionsRequest orderRouteSplitWarehouse) {
        //修改分仓规则
        long flag = orderRouteSplitWarehouseMapper.update(orderRouteSplitWarehouse);
        //先删除增匹配条件
        OrderRouteSplitWarehouseConditionRequest condition = new OrderRouteSplitWarehouseConditionRequest();
        condition.setRouteSplitWarehouseId(orderRouteSplitWarehouse.getId());
        condition.setDeleteFlag(1);
        orderRouteSplitWarehouseConditionMapper.update(condition);
        //新增匹配条件
        List<OrderRouteSplitWarehouseConditionRequest> conditions = orderRouteSplitWarehouse.getMatchCondition();
        for (OrderRouteSplitWarehouseConditionRequest orderRouteSplitWarehouseConditionRequest : conditions) {
            if (orderRouteSplitWarehouseConditionRequest != null) {
                orderRouteSplitWarehouseConditionRequest.setCreator(orderRouteSplitWarehouse.getCreator());
                orderRouteSplitWarehouseConditionRequest.setUpdater(orderRouteSplitWarehouse.getCreator());
                orderRouteSplitWarehouseConditionRequest.setDeleteFlag(0);
                orderRouteSplitWarehouseConditionRequest.setRouteSplitWarehouseId(orderRouteSplitWarehouse.getId());
            }
        }
        orderRouteSplitWarehouseConditionMapper.insertForeach(conditions);
        return flag;
    }

    @Override
    public OrderRouteSplitWarehouseResponse queryByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public List<OrderRouteSplitWarehouseResponse> query(OrderRouteSplitWarehouseRequest orderRouteSplitWarehouse) {
        return null;
    }

    @Override
    public PageResult<OrderRouteSplitWarehouseResponse> queryPage(OrderRouteSplitWarehouseRequest orderRouteSplitWarehouse, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<OrderRouteSplitWarehouseResponse> list = orderRouteSplitWarehouseMapper.queryPage(startIndex, pageQuery.getLimit(), orderRouteSplitWarehouse);
        long count = orderRouteSplitWarehouseMapper.queryPageCount(orderRouteSplitWarehouse);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

}
